package udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UdpServer {

	public static void main(String[] args) {
		try {
			DatagramSocket socket=new DatagramSocket(8888);
			System.out.println("服务器启动");
			byte[] sendData=new byte[1024];
			//接收数据
			DatagramPacket dp=new DatagramPacket(sendData, sendData.length);
			socket.receive(dp);
			System.out.println(new String(sendData));
			//发送数据
			InetAddress inetaddress=dp.getAddress();
			int port=dp.getPort();
			System.out.println(inetaddress+"==="+port);
			DatagramPacket datap=new DatagramPacket("我是服务端发送数据".getBytes("utf-8"), "我是服务端发送数据".getBytes("utf-8").length,inetaddress,port);
			socket.send(datap);
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
